package top.lilele.adminSystem.web;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lilele.adminSystem.dto.IdParams;
import top.lilele.adminSystem.dto.ProjectListDto;
import top.lilele.adminSystem.entity.AdminProjectEntity;
import top.lilele.adminSystem.numEnum.ProjectStatusEnum;
import top.lilele.adminSystem.service.AdminProjectService;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;
import top.lilele.adminSystem.vo.LogVo;
import top.lilele.adminSystem.vo.ProjectInfoVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilele
 */
@RestController
@RequestMapping(value = "project")
@Api(tags = "项目管理")
public class AdminProjectController {
    @Resource
    AdminProjectService adminProjectService;

    @ApiOperation(value = "新增项目")
    @PostMapping(value = "add")
    public BaseResponse add(@RequestBody @Validated AdminProjectEntity adminProjectEntity) {
        adminProjectEntity.setCreatedId(StpUtil.getLoginIdAsString());
        adminProjectService.add(adminProjectEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @ApiOperation(value = "查询项目列表")
    @PostMapping(value = "queryProjectList")
    public BaseResponse queryProjectList(@RequestBody ProjectListDto projectListDto) {
        return BaseResponse.info(ResponseCodeEnum.Success, adminProjectService.queryProjectList(projectListDto));
    }

    @ApiOperation(value = "查询项目详情")
    @PostMapping(value = "queryProjectDetail")
    public BaseResponse queryProjectDetail(@RequestBody @Validated IdParams idParams) {
        ProjectInfoVo projectInfoVo = adminProjectService.queryProjectDetail(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, projectInfoVo);
    }

    @ApiOperation(value = "查询项目状态枚举")
    @GetMapping(value = "queryProjectStatusList")
    public BaseResponse queryProjectStatusList() {
        List<Map> list = new ArrayList<>();
        ProjectStatusEnum[] values = ProjectStatusEnum.values();
        for (ProjectStatusEnum value : values) {
            Map map = new HashMap<>();
            map.put("key", value.getType());
            map.put("value", value.getMessage());
            list.add(map);
        }
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    @ApiOperation(value = "根据id更新项目信息")
    @PostMapping(value = "updateProjectById")
    public BaseResponse updateProjectById(@RequestBody @Validated AdminProjectEntity adminProjectEntity) {
        adminProjectService.updateProjectById(adminProjectEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @ApiOperation(value = "查询项目历史记录")
    @PostMapping(value = "queryProjectHistoryList")
    public BaseResponse queryProjectHistoryList(@RequestBody @Validated IdParams idParams) {
        List<LogVo> list = adminProjectService.queryProjectHistoryList(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    @ApiOperation(value = "查询所有的项目树结构")
    @PostMapping(value = "queryProjectListTree")
    public BaseResponse queryProjectListTree() {
        List<Tree<String>> trees = adminProjectService.queryProjectListTree();
        return BaseResponse.info(ResponseCodeEnum.Success, trees);
    }
}
